<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Asubscribers extends Controller_Abase_Adefaultactions {

    public function showMailerForm() {
        $actionLink = '/administration/'.$this->request->controller().'/'.$this->request->param('page').'/send';

        return View::factory('aforms/amailerForm', array(
            'actionLink' => $actionLink,
        ));
    }

    public function action_send() {
        $config = Kohana::$config->load('email');
        Email::connect($config);

        if ($this->request->post('type') == 0) {
            $to = ORM::factory($this->request->controller())->find_all()->as_array(NULL, 'email');
        } else {
            $to = ORM::factory($this->request->controller())->where('type', '=', $this->request->post('type'))->find_all()->as_array(NULL, 'email');
        }
        
        array_unshift($to, 'self@bell.kz');
        unset($to[0]);
        $from = 'noreply@zuber.kz';
        $subject = $this->request->post('subject');
        $message = $this->request->post('content');

        Email::send($to, $from, $subject, $message, $html = true);
        $this->redirect('administration/'.$this->request->controller().'/'.$this->request->param('page'));
    }

    public function showDefaultList() {
        $params = $this->params();
        $currentPage = $this->request->param('page');

        if ($currentPage == 1 || empty($currentPage)) {
            $this->offset = 0;
        }
        else {
            $this->offset = $currentPage*$this->_config->get('itemsPerPage')-$this->_config->get('itemsPerPage');
        }

        if (!empty($params['sortable'])) $sortable = TRUE; else $sortable = FALSE; // нужна ли сортировка
        if (!empty($params['order'])) { // указываем поле сортировки при выводе списка (если нет, то сортируем по id DESC)
            $order_field = $params['order']['field'];
            $order_method = $params['order']['method'];
        } else {
            $order_field = 'id';
            $order_method = 'DESC';
        }

        $this->template->right = View::factory('apages/amain', array(
            'title' => $params['main_title'],
            'content' => View::factory('apieces/acontent/asubscribersContent', array(
                'list' => View::factory('apieces/abase/adefaultList', array(
                    'defaults' => $this->dataListDefaults(),
                    'data' => ORM::factory($this->request->controller())->order_by('id', 'DESC')->limit($this->_config->get('itemsPerPage'))->offset($this->offset)->with('work')->with('admin')->find_all(),
                    'sortable' => $sortable,
                    'waymark' => $params['waymark'],
                )),
                'form' => $this->form,
                'mailer_form' => $this->showMailerForm(),
            )),
            'title' => $params['main_title'],
            'search_indexes' => !empty($params['search_indexes']) ? $params['search_indexes'] : '',
            'table' => $params['table'],
            'model' => $params['model']
        ));
    }

/*	public function action_index() {
        parent::action_index();
        $this->showMailerForm();
	}

    public function action_edit() {
        parent::action_edit();
        $this->showMailerForm();
    }*/

}